package com.example.lib5.company_interface.find_the_same_number;

/**
 * 创建日期：2022/6/9 0009 18:15
 *
 * @author tony.sun
 * 类说明：
 */
public class Main2 {
    public static void main(String[] args) {
        CirCleLinkedList cirCleLinkedList = new CirCleLinkedList();
        int m=3,n=4;
        Boy boy=cirCleLinkedList.count(m,n);
        if (boy!=null) {
            System.out.println(boy);
        }
    }
}
class CirCleLinkedList{
    Boy first;
    public Boy count(int count, int nums) {
        if (count<=0||nums<=0) {
            System.out.println("输入不合理");
            return null;
        }
        addBoy(nums);
        Boy hel=first;
        if (hel.getNext()!=first) {
            hel=hel.getNext();
        }
        while (true) {
            if (first==hel) {
                return hel;
            }
            for (int i = 0; i < count-1; i++) {
                hel=hel.getNext();
                first=first.getNext();
            }
            first=first.getNext();
            hel.next=first;
        }
    }

    private void addBoy(int nums) {
        Boy cub=null;
        for (int i = 0; i < nums; i++) {
            Boy boy = new Boy(i);
            if (first==null){
                first=boy;
                boy.next=first;
                cub = first;
            }else {
                cub.next=boy;
                boy.next=first;
                cub=boy;
            }
        }
    }
}
class Boy{
    Boy next;
    int no;

    public Boy(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "no=" + no +
                '}';
    }
};
